<template>
	<view class="viewcontent">
		<view class="viewcontenthead">
			<view class="fl" @click="$UTILS.goBack(1)"></view>
			<view class="fr" style="color: #fdf5fa;"> 箱信息绑定</view>
			<view class="leftitem"></view>
		</view>
		<view class="viewbtn">
			<view class="btnitem">
				<uni-data-select style="width: 100%;height: 100%;" name="select" :localdata="range"
					v-model="valuepower" @change="change($event)" placeholder="请输入功率">
				</uni-data-select>
			</view>
			<!-- <view class="btnitem">
				<u-button class="simpitem" @click="setpower('33')">设置大功率</u-button>
			</view> -->
			<view class="btnitem">
				<u-button class="simpitem" @click="setpower">设置功率</u-button>
			</view>
			<view class="btnitem">
				<u-button class="simpitem" @click="equimentget">箱组获取</u-button>
			</view>
		</view>
		<view class="viewcontenmes">
			<view class="searchitem">
				<view class="easyinputmes"><uni-easyinput v-model="value" placeholder="请输入内容"
						@input="input"></uni-easyinput></view>
				<view class="easyinputimg">
					<image @click="searchdemo" src="../../../static/Search.svg" style="height:52rpx;width: 52rpx;">
					</image>
				</view>
			</view>
			<view class="listitem">
				<view class="listitemes" @click="checkeditem(index)" v-for="(item,index) in this.itemlist" :key="index"
					:style="index==selectindex?'border: 1px solid rgba(0, 255, 127, 0.8)':''">
					<view class="listitemes1">
						<view class="chuan">
							<view>序号：{{index+1}}</view>
							<view class="easyinputimg">
								<image @click="uploads(item,index)" src="../../../static/sc.svg"
									style="height:52rpx;width: 52rpx;"></image>
							</view>
						</view>
						<view>物资名称：{{item.boxName}}</view>
						<view>物资编码：{{item.boxId}}</view>
					</view>
					<view class="listitemes2">
						<view class="listitemes2wrap">
							<view class="listitemes2wrapitemleft">
								<view class="wrapitemleftRFID">
									<view>RFID：{{item.rfid}}</view>
								</view>
							</view>
							<view class="listitemes2wrapitemright">
								<!--<u-button  @click="setRFID(index)">箱子赋码</u-button>-->
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<myLoading v-if="loading" style="position: absolute;z-index: 9;"></myLoading>
		<keyboard-listener @keydown="onKeydown"></keyboard-listener>
		<keyboard-listener @keyup="onKeyup"></keyboard-listener>
	</view>
</template>

<script>
	import keyboardListener from '../../../components/uniapp-keyboard-listener/src/keyboard-listener.vue'
	// #ifdef APP-PLUS
	// const pda = uni.requireNativePlugin("PDAModule");
	// const tone = uni.requireNativePlugin("ToneModule");
	// var log = uni.requireNativePlugin("LogModule")
	// #endif
	let andoridModule = uni.requireNativePlugin('seuic-uhf');
	export default {
		onLoad() {
			if (this.$UTILS.getunlineBoxinformationbinding()) {
				this.itemlist = JSON.parse(this.$UTILS.getunlineBoxinformationbinding())
			}
		},
		components: {
			keyboardListener
		},
		data() {
			return {
				itemlist: [],
				selectindex: 0,
				value: '',
				lastQueryTimestamp: 0,
				loading: false,
				valuepower:'',
				range:[{
						value: '1',
						text: "1"
					},
					{
						value: '2',
						text: "2"
					},
					{
						value: '3',
						text: "3"
					},
					{
						value: '4',
						text: "4"
					},
					{
						value: '5',
						text: "5"
					},
					{
						value: '6',
						text: "6"
					},
					{
						value: '7',
						text: "7"
					},
					{
						value: '8',
						text: "8"
					},
					{
						value: '9',
						text: "9"
					},
					{
						value: '10',
						text: "10"
					},
					{
						value: '11',
						text: "11"
					},
					{
						value: '12',
						text: "12"
					},
					{
						value: '13',
						text: "13"
					},
					{
						value: '14',
						text: "14"
					},
					{
						value: '15',
						text: "15"
					},
					{
						value: '16',
						text: "16"
					},
					{
						value: '17',
						text: "17"
					},
					{
						value: '18',
						text: "18"
					},
					{
						value: '19',
						text: "19"
					},
					{
						value: '20',
						text: "20"
					},
					{
						value: '21',
						text: "21"
					},
					{
						value: '22',
						text: "22"
					},
					{
						value: '23',
						text: "23"
					},
					{
						value: '24',
						text: "24"
					},
					{
						value: '25',
						text: "25"
					},
					{
						value: '26',
						text: "26"
					},
					{
						value: '27',
						text: "27"
					},
					{
						value: '28',
						text: "28"
					},
					{
						value: '29',
						text: "29"
					},
					{
						value: '30',
						text: "30"
					},
					{
						value: '31',
						text: "31"
					},
					{
						value: '32',
						text: "32"
					}
				]
				
			};
		},
		methods: {
			change(e){
				this.valuepower = e
				andoridModule.setPower(this.valuepower, (e) => {
					this.templedata = e
				});
				
			},
			setpower() {
				if(this.valuepower == ''){
					this.$UTILS.toast('请选择功率')
					return
				}
				this.$UTILS.toast('设置功率成功')
			},
			async equimentget() {
				this.init()
			},
			// setpower(e1) {
			// 	andoridModule.setPower(e1, (e) => {
			// 		this.templedata = e
			// 	});
			// 	this.$UTILS.toast('设置功率成功')
			// },
			async uploads(item, index) {
				let demoitem = JSON.stringify(item)
				this.loading = true
				let [error, res] = await this.$UTILS.requestRest(this.$COMMON.REST_URL.SETBOXLIST, 'POST', demoitem)
				if (res && res.statusCode == 200) {
					if (res.data?.code == 401) {
						let jqdizhi = this.$UTILS.getunlinelogin()
						let ictdizhi = this.$UTILS.getunlineaddress()
						uni.clearStorageSync()
						this.$UTILS.setunlinelogin(jqdizhi);
						this.$UTILS.setunlineaddress(ictdizhi);
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/view-layout/login/login',
							});
						}, 500)
						
						this.$UTILS.toast('token过期，请从新登录')
						return
					}
					this.loading = false
					this.$UTILS.toast('数据上传成功')
					this.init()
				} else {
					this.loading = false
					this.$UTILS.toast('数据上传失败')
					this.init()
				}
			},
			async init() {
				
				let [error, res] = await this.$UTILS.requestRest(this.$COMMON.REST_URL.BOXLIST, 'GET')
				if (res && res.statusCode == 200) {
					if (res.data?.code == 401) {
						let jqdizhi = this.$UTILS.getunlinelogin()
						let ictdizhi = this.$UTILS.getunlineaddress()
						uni.clearStorageSync()
						this.$UTILS.setunlinelogin(jqdizhi);
						this.$UTILS.setunlineaddress(ictdizhi);
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/view-layout/login/login',
							});
						}, 500)
						
						this.$UTILS.toast('token过期，请从新登录')
						return
					}
					this.itemlist = res.data.result
					this.loading = true
					setTimeout(() => {
						this.$UTILS.setunlineBoxinformationbinding(JSON.stringify(res.data.result))
						this.loading = false
						this.$UTILS.toast('获取箱组成功')
					}, 500)
				}else{
					this.$UTILS.toast('获取箱组失败')
					return
				}

				

			},
			input(e) {
				this.value = e
			},
			async searchdemo() {
				let [error, res] = await this.$UTILS.requestRest(this.$COMMON.REST_URL.BOXLIST, 'GET')
				if (res.data?.code == 401) {
					let jqdizhi = this.$UTILS.getunlinelogin()
					let ictdizhi = this.$UTILS.getunlineaddress()
					uni.clearStorageSync()
					this.$UTILS.setunlinelogin(jqdizhi);
					this.$UTILS.setunlineaddress(ictdizhi);
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/view-layout/login/login',
						});
					}, 500)
					
					this.$UTILS.toast('token过期，请从新登录')
					return
				}
				if (this.value == '') {
					this.itemlist = res.data.result
					return
				}
				this.itemlist = res.data.result.filter(item => {
					if (item.boxName) {
						return item.boxName.includes(this.value)
					}

				})
			},
			checkeditem(index) {
				this.selectindex = index
			},
			setRFID(index) {
				this.itemlist[index].RFID = '123'
				this.$UTILS.setBoxinformationbinding(this.itemlist)

			},
			onKeydown(e) {
				if (e.keyCode == 123) {
					andoridModule.inventoryOnce(1000, (e) => {
						if (e != '未寻到') {
							let jsonResult = JSON.parse(JSON.stringify(e))
							this.itemlist[this.selectindex].rfid = jsonResult.id
							this.loading = true
							setTimeout(() => {
								this.$UTILS.setunlineBoxinformationbinding(JSON.stringify(this.itemlist))
								this.loading = false
							}, 500)
						} else {
							this.templedata = e
						}
					});
				}


			},
			onKeyup(e) {
				// pda.stop()
				// // 蜂鸣器
				// tone.setToneTimes(1, 100, 100)
				andoridModule.inventoryStop((e) => {
					this.templedata = e
				});
			},
			processData(result) {
				let jsonResult = JSON.parse(result);
				this.itemlist[this.selectindex].rfid = jsonResult.epc
			}
		}
	}
</script>

<style lang="scss">
	.viewcontent {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		padding-top: 55rpx;

		.viewcontenthead {
			width: 100%;
			height: 5%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.fl {
				width: 100rpx;
				height: 10vh;
				line-height: 10vh;
				background: url("/static/right-arrow.png") no-repeat center;
				background-size: 14%;
				transform: rotate(180deg);
				cursor: pointer; // 增加鼠标指针样式
			}

			.fr {
				cursor: pointer; // 增加鼠标指针样式
			}

			.leftitem {
				width: 100rpx;
				display: flex;
				justify-content: center;
				z-index: 999;
			}
		}

		.viewcontenmes {
			width: 100%;
			height: 90%;

			::v-deep .uni-input-input {
				color: rgba(250, 249, 249, 0.8) !important;
			}

			.searchitem {
				width: 100%;
				height: 10%;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;

				.easyinputmes {
					width: 660rpx;
					box-sizing: border-box;
					padding: 0 25rpx;
				}

				.easyinputimg {
					width: 80rpx;
				}
			}

			.listitem {
				width: 100%;
				height: 90%;
				overflow: auto;
				display: flex;
				flex-direction: column;

				.listitemes {
					border: 1px solid white;
					border-radius: 15px;
					width: 98%;
					height: 250rpx;
					min-height: 250rpx;
					margin: 0 auto;
					margin-top: 15rpx;
					box-sizing: border-box;
					padding: 0 5rpx;
					font-family: PingFang SC;
					font-weight: 550;
					color: rgba(250, 249, 249, 0.8);
					font-size: 16px;

					.listitemes1 {
						width: 100%;
						height: 80%;
						box-sizing: border-box;
						padding-left: 55rpx;

						.chuan {
							display: flex;
							justify-content: space-between;
							box-sizing: border-box;
							padding-right: 20rpx;
						}

						view {
							height: 33%;
							display: flex;
							align-items: center;
						}
					}

					.listitemes2 {
						width: 100%;
						height: 20%;
						box-sizing: border-box;
						padding-left: 55rpx;

						.listitemes2wrap {
							width: 100%;
							height: 100%;
							display: flex;
							flex-direction: row;

							.listitemes2wrapitemleft {
								width: 99%;
								height: 100%;
								display: flex;
								align-items: center;

								.wrapitemlefttag {
									width: 100%;
									height: 50%;
									display: flex;
									align-items: center;

									view {
										text-overflow: ellipsis;
										overflow: hidden;
										white-space: nowrap;
									}
								}

								.wrapitemleftRFID {
									width: 100%;
									height: 50%;
									display: flex;
									align-items: center;

									view {
										text-overflow: ellipsis;
										overflow: hidden;
										white-space: nowrap;
									}
								}
							}

							.listitemes2wrapitemright {
								width: 1%;
								height: 100%;
								display: flex;
								align-items: center;
							}
						}
					}
				}
			}
		}
	}

	.viewbtn {
		width: 100%;
		height: 5%;
		display: flex;
		justify-content: space-around;
		align-items: center;
		box-sizing: border-box;
		padding: 0 15rpx;

		.btnitem {
			width: 30%;
			height: 100%;

			.simpitem {
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.btnitempower{
			width: 60%;
			height: 100%;
		}
	}
</style>